#ifndef _WIFI_FOR_STATION_H
#define _WIFI_FOR_STATION_H
#include <stdio.h>
#include "esp_wifi.h"
#include "freertos/FreeRTOS.h"
#include "nvs_flash.h"
#include "esp_event.h"
#include <string>
#include <functional>

typedef enum
{
  WIFI_CONNECTED,
  WIFI_DISCONNECTED,
} wifi_status_t;

class WiFi
{
private:
  std::string ssid;
  std::string password;
  std::string ap_ssid = "";
  std::string ap_password = "";
  wifi_status_t status = wifi_status_t::WIFI_DISCONNECTED;
  std::function<void(wifi_status_t)> status_cb = nullptr;
  std::string sta_ip = "";

  bool sta_init = false;
  bool ap_init = false;

  WiFi(const std::string &ap_ssid = "ESP32", const std::string &ap_password = "", const std::string &ssid = "", const std::string &password = "");
  ~WiFi();

  WiFi(const WiFi &) = delete;
  WiFi &operator=(const WiFi &) = delete;
  WiFi(WiFi &&) = delete;
  WiFi &operator=(WiFi &&) = delete;

public:
  static WiFi &getInstance()
  {
    static WiFi instance;
    return instance;
  }

  void setAp(const std::string &ap_ssid, const std::string &ap_password);
  esp_err_t start();
  esp_err_t connect();
  esp_err_t connect(const std::string &ssid, const std::string &password);
  esp_netif_t *staInit();
  esp_netif_t *apInit();

  void onStatusChange(std::function<void(wifi_status_t)> cb)
  {
    status_cb = cb;
  }

  esp_err_t setMode(wifi_mode_t mode);
  wifi_mode_t getMode();
  wifi_status_t getStatus();
  std::string getWiFiList(void);

  std::string &getIP();
  esp_err_t deinit();
};

#endif
